import { createApp } from 'vue';
import CustomConfirm from '../components/CustomConfirm.vue';

const confirm = (options = {}) => {
  return new Promise((resolve, reject) => {
    // 创建一个div作为挂载点
    const mountNode = document.createElement('div');
    document.body.appendChild(mountNode);

    // 创建confirm组件实例
    const app = createApp(CustomConfirm, {
      ...options,
      visible: true,
      onConfirm: () => {
        // 确认按钮点击事件
        app.unmount();
        document.body.removeChild(mountNode);
        resolve(true);
      },
      onCancel: () => {
        // 取消按钮点击事件
        app.unmount();
        document.body.removeChild(mountNode);
        resolve(false);
      }
    });

    // 挂载组件
    app.mount(mountNode);
  });
};

export default confirm;